#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash rmblast-2.13.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
yum install -y wget tar patch environment-modules

#download ncbi-blast+
wget -t 0 -c -P $buildpath https://ftp.ncbi.nlm.nih.gov/blast/executables/blast+/2.13.0/ncbi-blast-2.13.0+-src.tar.gz
tar -xzvf $buildpath/ncbi-blast-2.13.0+-src.tar.gz -C $buildpath && rm -rf $buildpath/ncbi-blast-2.13.0+-src.tar.gz
#download patch for rmblast
wget -t 0 -c -P $buildpath http://www.repeatmasker.org/isb-2.10.0+-rmblast.patch.gz
gunzip $buildpath/isb-2.10.0+-rmblast.patch.gz
#build rmblast
cd $buildpath/ncbi-blast-2.13.0+-src
patch -p1 < ../isb-2.10.0+-rmblast.patch
cd c++
CC=clang CXX=clang++ ./configure --with-mt --without-debug --without-krb5 --without-openssl --with-projects=scripts/projects/rmblastn/project.lst --prefix=$installpath/rmblast-2.13.0
make -j$(nproc) && make install

cat>"$installpath/rmblast-2.13.0/rmblast_modulefiles"<<EOF
#%Module1.0
conflict rmblast
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
        set modfile_path [file dirname \$modfile]
            return \$modfile_path
    }
set pwd [getModulefileDir]
set RMBLAST \$pwd
setenv RMBLAST \$RMBLAST
prepend-path PATH \$RMBLAST/bin
EOF

rm -rf $buildpath/ncbi-blast-2.13.0+-src

